/*
 * The MIT License
 *
 * Copyright 2015 usuario.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package com.chronoserp.controller;

import com.chronoserp.modulo.estoque.view.CadAlmoxarifado;
import com.chronoserp.modulo.estoque.view.ConsAlmoxarifado;
import com.chronoserp.entidades.Empresa;
import com.chronoserp.entidades.Usuario;
import com.chronoserp.modulo.estoque.servidor.CadAlmoxarifadoAction;
import com.chronoserp.padrao.cliente.Container;
import com.chronoserp.padrao.java.Constantes;
import javax.swing.JOptionPane;
import org.openswing.swing.form.client.FormController;
import org.openswing.swing.mdi.client.MDIFrame;
import org.openswing.swing.message.receive.java.Response;
import org.openswing.swing.message.receive.java.ValueObject;
import org.openswing.swing.util.client.ClientUtils;
import org.openswing.swing.util.java.Consts;

/**
 *
 * @author usuario
 */
public class ACadController extends FormController {

    private CadAlmoxarifado form;
    private ConsAlmoxarifado grid;
    private String acaoServidor;
    private String pk;

    public ACadController(ConsAlmoxarifado grid, String pk) {
        this.grid = grid;
        this.pk = pk;
        this.acaoServidor = "cadAlmoxarifadoAction";
        //TODO: teste
//        form = new CadAlmoxarifado(this);
        form.setParentFrame(this.grid);
        form.pushFrame(form);
        MDIFrame.add(form);

        if (pk != null) {
            form.getForm1().setMode(Consts.READONLY);
            form.getForm1().reload();
        } else {
            form.getForm1().setMode(Consts.INSERT);
        }
    }

    @Override
    public Response loadData(Class valueObjectClass) {
        Usuario usr = (Usuario)Container.getContainer().get("usuario");
        if (Container.isTresCamadas()) {
            return ClientUtils.getData(acaoServidor, new Object[]{Constantes.LOAD, pk,usr});

        } else {
            CadAlmoxarifadoAction acao = new CadAlmoxarifadoAction();
            return acao.executeCommand(new Object[]{Constantes.LOAD, pk,usr}, null, null, null, null, null);
        }
    }

    @Override
    public Response insertRecord(ValueObject newPersistentObject) throws Exception {
        Empresa emp;
        emp = (Empresa) Container.getContainer().get("empresa");
        Usuario usr = (Usuario)Container.getContainer().get("usuario");
        if (Container.isTresCamadas()) {
            return ClientUtils.getData(acaoServidor, new Object[]{Constantes.INSERT, newPersistentObject,usr});
        } else {
            CadAlmoxarifadoAction acao = new CadAlmoxarifadoAction();
            return acao.executeCommand(new Object[]{Constantes.INSERT, newPersistentObject,usr}, null, null, null, null, null);
        }
    }

    @Override
    public Response updateRecord(ValueObject oldPersistentObject, ValueObject persistentObject) throws Exception {
        Usuario usr = (Usuario)Container.getContainer().get("usuario");
        
        if (Container.isTresCamadas()) {
            return ClientUtils.getData(acaoServidor, new Object[]{Constantes.UPDATE, oldPersistentObject, persistentObject,usr});

        } else {
            CadAlmoxarifadoAction acao = new CadAlmoxarifadoAction();
            return acao.executeCommand(new Object[]{Constantes.UPDATE, oldPersistentObject, persistentObject,usr}, null, null, null, null, null);
        }
    }

    @Override
    public void afterInsertData() {
        JOptionPane.showMessageDialog(form, "Transação realizada com sucesso !", "Informacao do Sistema", JOptionPane.INFORMATION_MESSAGE);
        form.dispose();
        grid.getGrid1().reloadData();
    }

    @Override
    public void afterEditData() {
        JOptionPane.showMessageDialog(form, "Transação realizada com sucesso !", "Informacao do Sistema", JOptionPane.INFORMATION_MESSAGE);
        form.dispose();
        grid.getGrid1().reloadData();
    }

    @Override
    public void afterDeleteData() {
        JOptionPane.showMessageDialog(form, "Transação realizada com sucesso !", "Informacao do Sistema", JOptionPane.INFORMATION_MESSAGE);
    }

}
